package sv;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class OutputWindow extends JFrame implements KeyListener{
	private Pan p;
	private ArrayList<Integer>[][] matrix = new ArrayList[9][9];
	private ToolBox tb;
	private int[][] input;
	private int ri = -1, co = -1;
	private int w=341;
	private int xZ = 5;
	private int sq = 3;
	private int ssp = 4;
	private int ssg = 7;
	private int gq = (w-2*xZ)/9;
	private int h = gq*9+20;
	private int nS;

	/**
	 * Classe che definisce la finestra grafica contenente la griglia del sudoku
	 * 
	 * @param title
	 */

	public OutputWindow(String title,int nS) {
		tb = new ToolBox();
		input = tb.getInput(nS);
		this.nS=nS;
		
		addKeyListener(this);

		setSize(w, h);
		setLocation(1,23);
		setTitle(title);

		p = new Pan();
		add(p);

		setBackground(Color.white);

		setResizable(false);
		setUndecorated(true);
		//setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}

	/**
	 * Classe che definisce la struttura Pan inserita in OutputWindow
	 * 
	 * @author Dario
	 * 
	 */
	class Pan extends JPanel {
		public void paint(Graphics g) {
			Graphics2D g2 = (Graphics2D) g;
			
			g2.setStroke(new BasicStroke(3));
			g2.setColor(tb.getColor(nS));
			g2.drawRect(0,0,w-2,h-2);
			
			g2.setStroke(new BasicStroke(1));
			
			g.setColor(Color.gray);
			for (int r = 0; r < 9; r++) {
				for (int c = 0; c < 9; c++) {
					if (input[r][c] != 0) {
						g.setColor(new Color(100, 149, 237));
						//g.setColor(Color.blue);
						g.fillRect(c * gq + xZ+sq, r * gq + 13, gq-2*sq, gq-2*sq);
					}
					if (r == ri && c == co) {
						//g.setColor(new Color(100, 149, 237));
						//g.setColor(Color.cyan);
						g.setColor(new Color(30, 144, 255));
						g.fillRect(c * gq + xZ, r * gq + 10, gq, gq);
					}
					g.setColor(Color.black);
					g.drawRect(c * gq + xZ, r * gq + 10, gq, gq);
					if (matrix[r][c] != null) {
						if (matrix[r][c].size() == 1) {
							//g.setColor(Color.darkGray);
							g.setColor(new Color(66, 66, 66));
							if (input[r][c] != 0) {
								g.setColor(Color.white);
								//g.setColor(new Color(17, 255, 0));
							}
							g.setFont(new Font("Helvetica", Font.PLAIN, 40));
							g.drawString(matrix[r][c].get(0) + "", c * gq + xZ + ssg,
									r * gq + 44);
						} else {
							g.setFont(new Font("Helvetica", Font.PLAIN, 12));
							for (int k = 0; k < matrix[r][c].size(); k++) {
								g.drawString(matrix[r][c].get(k) + "", c * gq
										+ xZ + ssp + k % 3 * 11, r * gq + 23 + k / 3
										* 11);
							}
						}
					}
				}
			}

			g2.setStroke(new BasicStroke(3));
			g2.setColor(Color.gray);
			for (int i = 0; i < 3; i++)
				for (int j = 0; j < 3; j++) {
					Rectangle2D rec = new Rectangle2D.Double(i * 3 * gq + xZ,
							j * 3 * gq + 10, gq * 3, gq * 3);
					g2.draw(rec);
				}
		}
	}

	/**
	 * Metodo che assegna i valori agli indici delle righe e colonne
	 * 
	 * @param r
	 *            indice riga
	 * @param c
	 *            indice colonna
	 */
	public void setCas(int r, int c) {
		ri = r;
		co = c;
	}

	/**
	 * Metodo che assegna i possibili valori a alla cella di indici:
	 * 
	 * @param r
	 *            indice riga
	 * @param c
	 *            indice colonna
	 * @param v
	 *            array contenente i valori e aggiorna la grafica in out
	 */
	public void putElement(int r, int c, ArrayList<Integer> v) {
		matrix[r][c] = v;
		repaint();
	}
	
	public void setPosition(int x,int y){
		setBounds(x,y,w,h);
	}
	
	public void keyPressed(KeyEvent e){
		int codice = e.getKeyCode();
		String tasto = e.getKeyText(codice);
		ToolBox.setDelay(tasto);
	}
	
	public void keyReleased(KeyEvent e){}
	public void keyTyped(KeyEvent e){}
}
